{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T09:50:35.329295Z",
     "start_time": "2025-09-13T09:50:35.293971Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "1ed20b56573606b9",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:00.874874Z",
     "start_time": "2025-09-13T11:13:00.862950Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 整数型数组\n",
    "arr1 = np.array([1,2,3,4,5])  # 全部是整数\n",
    "arr1"
   ],
   "id": "8b5b784c5911929b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:06.869903Z",
     "start_time": "2025-09-13T11:13:06.866545Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 浮点型数组\n",
    "arr2 = np.array([1.0, 2, 3, 4])\n",
    "arr2"
   ],
   "id": "f59e17c0cef8a3e2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:33.105010Z",
     "start_time": "2025-09-13T11:13:33.098217Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改数据 发生 截断/升级\n",
    "arr1[0] = 2.3\n",
    "print(arr1)\n",
    "\n",
    "arr2[1] = 3\n",
    "arr2"
   ],
   "id": "2a2f141e976fae9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 2 3 4 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1., 3., 3., 4.])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:39.550766Z",
     "start_time": "2025-09-13T11:13:39.546100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 转换\n",
    "arr3 = arr1.astype(float)\n",
    "arr3"
   ],
   "id": "eb2493ee47d2f0f2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:45.563756Z",
     "start_time": "2025-09-13T11:13:45.560447Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr4 = arr2.astype(int)\n",
    "arr4"
   ],
   "id": "23633d70f7d6a77a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 3, 4])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:50.479496Z",
     "start_time": "2025-09-13T11:13:50.476325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用np.ones()创建多维数组\n",
    "arr1 = np.ones(4)\n",
    "arr1"
   ],
   "id": "6e5e636c5007ddc2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1.])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:13:54.527575Z",
     "start_time": "2025-09-13T11:13:54.524304Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.ones((1, 4))\n",
    "arr2"
   ],
   "id": "63111637d0699ac4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:14:19.431392Z",
     "start_time": "2025-09-13T11:14:19.427793Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr3 = np.ones((1, 1, 4))\n",
    "arr3"
   ],
   "id": "2f6fd8aa60060a00",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:14:24.867225Z",
     "start_time": "2025-09-13T11:14:24.863709Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr1.shape)\n",
    "print(arr2.shape)\n",
    "print(arr3.shape)"
   ],
   "id": "efdddabd1a778031",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n",
      "(1, 4)\n",
      "(1, 1, 4)\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:09.028833Z",
     "start_time": "2025-09-13T11:16:09.022675Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 维度转换\n",
    "arr1 = np.array([1,2,3,4,5,6,7,8,9,10])\n",
    "arr1"
   ],
   "id": "b2388edce8b2cfd0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:22.084084Z",
     "start_time": "2025-09-13T11:16:22.079742Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 升级为二维数组\n",
    "# 可以写-1，会直接换算出来的\n",
    "arr2 = arr1.reshape((2, 5))  # 2行5列\n",
    "arr3 = arr1.reshape((1, -1))  # 1行10列\n",
    "arr4 = arr1.reshape((-1, 5))  # 2行5列\n",
    "print(arr2)\n",
    "print(arr3)\n",
    "arr4"
   ],
   "id": "fcf83fe308898888",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "[[ 1  2  3  4  5  6  7  8  9 10]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:32.582550Z",
     "start_time": "2025-09-13T11:16:32.578680Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机数组\n",
    "arr1 = np.random.random(10)\n",
    "arr1"
   ],
   "id": "57c7694e1f86df43",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.03964513, 0.02702095, 0.60810702, 0.08530007, 0.4964885 ,\n",
       "       0.50734843, 0.96839011, 0.6696311 , 0.72710017, 0.71682016])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:34.780710Z",
     "start_time": "2025-09-13T11:16:34.774687Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = 40 * np.random.random(10)\n",
    "arr1"
   ],
   "id": "ca177b7a8d57e15a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([21.65529759, 19.00499595, 30.68771918, 23.43477039,  4.58081147,\n",
       "        0.61690922, 27.26504868, 28.96215621, 25.68100775,  4.44697365])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:36.819031Z",
     "start_time": "2025-09-13T11:16:36.813475Z"
    }
   },
   "cell_type": "code",
   "source": "arr1.astype(int)",
   "id": "9ef14ec5a1918c9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([21, 19, 30, 23,  4,  0, 27, 28, 25,  4])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:37.870806Z",
     "start_time": "2025-09-13T11:16:37.864707Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.random.randint(1, 10, 5)\n",
    "arr1"
   ],
   "id": "c6ed7edd3591715d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 2, 8, 1, 6])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:16:39.186516Z",
     "start_time": "2025-09-13T11:16:39.181506Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.random.randint(1, 10, (1, 5))\n",
    "arr2  # 1行5列"
   ],
   "id": "4520f8ebe8b70f40",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 2, 8, 5]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:36:10.481753Z",
     "start_time": "2025-09-13T11:36:10.470085Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.random.randint(1, 10, (2, 6))\n",
    "arr2  # 2行6列"
   ],
   "id": "584ab0680807ea16",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 1, 2, 9, 8],\n",
       "       [5, 5, 1, 8, 5, 7]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-13T11:38:02.184753Z",
     "start_time": "2025-09-13T11:38:02.180815Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr3 = np.random.normal(0, 1, (2, 3))  # 均值为0，标准差为1，2行3列\n",
    "arr3"
   ],
   "id": "4f1e4217d44980ea",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.13230888, -0.13427897, -2.00830865],\n",
       "       [-0.22595673,  1.36132165,  2.41387619]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:34:42.286981Z",
     "start_time": "2025-09-14T08:34:42.275251Z"
    }
   },
   "cell_type": "code",
   "source": "arr3[0, 1]",
   "id": "c61d685c0a2ad864",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-0.13427897494006255)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:38:47.730730Z",
     "start_time": "2025-09-14T08:38:47.725004Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.arange(0, 100, 10)\n",
    "arr1"
   ],
   "id": "71f2087d7c3da8b2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:38:58.488922Z",
     "start_time": "2025-09-14T08:38:58.483045Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 花式索引\n",
    "arr1[[0, 6, 9]]"
   ],
   "id": "ac6fa42063bb1ed1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 60, 90])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:41:02.957791Z",
     "start_time": "2025-09-14T08:41:02.952311Z"
    }
   },
   "cell_type": "code",
   "source": "arr5 = np.arange(1, 17).reshape((4, 4))",
   "id": "30c6d5eb43c4c5c7",
   "outputs": [],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:41:12.780742Z",
     "start_time": "2025-09-14T08:41:12.776046Z"
    }
   },
   "cell_type": "code",
   "source": "arr5",
   "id": "cf317eb536b4721c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12],\n",
       "       [13, 14, 15, 16]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 66
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:42:53.900018Z",
     "start_time": "2025-09-14T08:42:53.894878Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 矩阵的花式索引\n",
    "arr5[[0, 3], [1, 2]]  # 0,1 -> 1行2列  3,2 -> 4行3列"
   ],
   "id": "9da326463c2e58c1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 15])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:44:32.032892Z",
     "start_time": "2025-09-14T08:44:32.027111Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 花式索引修改元素\n",
    "arr5[[0, 1, 2, 3], [3, 2, 1, 0]] = 100\n",
    "arr5"
   ],
   "id": "5abeac97ee5882ce",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3, 100],\n",
       "       [  5,   6, 100,   8],\n",
       "       [  9, 100,  11,  12],\n",
       "       [100,  14,  15,  16]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:49:36.970040Z",
     "start_time": "2025-09-14T08:49:36.963551Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 提取行\n",
    "arr5[0, :]"
   ],
   "id": "a4f13291eb0cd71d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   3, 100])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:49:51.072992Z",
     "start_time": "2025-09-14T08:49:51.068041Z"
    }
   },
   "cell_type": "code",
   "source": "arr5[0]",
   "id": "716a86dfa3d2c6a8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   3, 100])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:50:03.221035Z",
     "start_time": "2025-09-14T08:50:03.216523Z"
    }
   },
   "cell_type": "code",
   "source": "arr5[:, 0]",
   "id": "6f73122a348927e8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   5,   9, 100])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:50:31.737069Z",
     "start_time": "2025-09-14T08:50:31.732162Z"
    }
   },
   "cell_type": "code",
   "source": "arr5[:, 1:3]",
   "id": "feb0b4a4a3c378c8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  2,   3],\n",
       "       [  6, 100],\n",
       "       [100,  11],\n",
       "       [ 14,  15]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:52:13.376794Z",
     "start_time": "2025-09-14T08:52:13.373406Z"
    }
   },
   "cell_type": "code",
   "source": [
    "cut = arr5[0]\n",
    "cut"
   ],
   "id": "b13c539ad02625e5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   3, 100])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:52:49.667984Z",
     "start_time": "2025-09-14T08:52:49.662185Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 升级为矩阵\n",
    "cut = cut.reshape((1, -1))\n",
    "cut"
   ],
   "id": "85fd73f8da5aea0e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3, 100]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:58:19.725271Z",
     "start_time": "2025-09-14T08:58:19.715344Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 改为列矩阵\n",
    "cut = cut.T\n",
    "cut"
   ],
   "id": "f51cdf17a92b6d3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1],\n",
       "       [  2],\n",
       "       [  3],\n",
       "       [100]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T08:58:27.790801Z",
     "start_time": "2025-09-14T08:58:27.787872Z"
    }
   },
   "cell_type": "code",
   "source": [
    "cut = cut.T\n",
    "cut"
   ],
   "id": "b241a87d210bcba7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3, 100]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:05:05.989744Z",
     "start_time": "2025-09-14T09:05:05.981178Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.array([[1,2,3,4,5,6,7,8,9,10]])\n",
    "arr2 = np.arange(11,21).reshape((1, -1))\n",
    "arr2"
   ],
   "id": "b60534910ab81bb5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:05:10.234179Z",
     "start_time": "2025-09-14T09:05:10.228483Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr3 = np.concatenate([arr1, arr2], axis=0) # 行拼接 扩充行\n",
    "arr3"
   ],
   "id": "eacb3de40414a308",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:05:13.041920Z",
     "start_time": "2025-09-14T09:05:13.036697Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr4 = np.concatenate([arr1, arr2], axis=1)  # 列拼接 扩充列\n",
    "arr4"
   ],
   "id": "b222b6796200a1e6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "        17, 18, 19, 20]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:22:00.354870Z",
     "start_time": "2025-09-14T09:22:00.342601Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 矩阵和矩阵的运算\n",
    "arr1 = np.array([[1,2,3,4,5,6,7,8,9,10]])\n",
    "arr2 = np.arange(-11, -1).reshape((1, -1))\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "id": "be92742a48cc5b74",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5  6  7  8  9 10]]\n",
      "[[-11 -10  -9  -8  -7  -6  -5  -4  -3  -2]]\n"
     ]
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:22:14.934078Z",
     "start_time": "2025-09-14T09:22:14.930055Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 + arr2",
   "id": "927cf84253e5546",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-10,  -8,  -6,  -4,  -2,   0,   2,   4,   6,   8]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:22:35.122243Z",
     "start_time": "2025-09-14T09:22:35.119432Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 - arr2",
   "id": "8a6574a4b855e5cf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12, 12, 12, 12, 12, 12, 12, 12, 12, 12]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:22:42.288883Z",
     "start_time": "2025-09-14T09:22:42.283928Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 / arr2",
   "id": "f20acbfeefb446b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.09090909, -0.2       , -0.33333333, -0.5       , -0.71428571,\n",
       "        -1.        , -1.4       , -2.        , -3.        , -5.        ]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:22:58.449441Z",
     "start_time": "2025-09-14T09:22:58.444305Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 * arr2",
   "id": "61b5104b0bdaf898",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-11, -20, -27, -32, -35, -36, -35, -32, -27, -20]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:24:27.395788Z",
     "start_time": "2025-09-14T09:24:27.393115Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播\n",
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.arange(1, 13).reshape(4, 3)\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "id": "71f9b0a02a1a8f19",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "execution_count": 91
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:24:35.134445Z",
     "start_time": "2025-09-14T09:24:35.131574Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 + arr2",
   "id": "2da5b90d15b23a77",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [ 5,  7,  9],\n",
       "       [ 8, 10, 12],\n",
       "       [11, 13, 15]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 92
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:24:54.214477Z",
     "start_time": "2025-09-14T09:24:54.209567Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 - arr2",
   "id": "a3330206da4df39b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0],\n",
       "       [-3, -3, -3],\n",
       "       [-6, -6, -6],\n",
       "       [-9, -9, -9]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:28:06.470279Z",
     "start_time": "2025-09-14T09:28:06.465382Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr3 = np.array([3, 4, 5, 6]).reshape((-1, 1))\n",
    "print(arr3)"
   ],
   "id": "99c791bc92b9f0d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3]\n",
      " [4]\n",
      " [5]\n",
      " [6]]\n"
     ]
    }
   ],
   "execution_count": 99
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:28:08.103371Z",
     "start_time": "2025-09-14T09:28:08.099985Z"
    }
   },
   "cell_type": "code",
   "source": "print(arr2)",
   "id": "77b55da339903eef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:28:09.585937Z",
     "start_time": "2025-09-14T09:28:09.581249Z"
    }
   },
   "cell_type": "code",
   "source": "arr3 + arr2",
   "id": "5714f2a83c997504",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6],\n",
       "       [ 8,  9, 10],\n",
       "       [12, 13, 14],\n",
       "       [16, 17, 18]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 101
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:28:28.636673Z",
     "start_time": "2025-09-14T09:28:28.631878Z"
    }
   },
   "cell_type": "code",
   "source": "arr3 - arr2",
   "id": "a7afd837913dacba",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  1,  0],\n",
       "       [ 0, -1, -2],\n",
       "       [-2, -3, -4],\n",
       "       [-4, -5, -6]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:30:28.191363Z",
     "start_time": "2025-09-14T09:30:28.185329Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.array([0, 1, 2])\n",
    "arr2 = arr1.reshape((-1, 1))\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "id": "e06b6833602568e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2]\n",
      "[[0]\n",
      " [1]\n",
      " [2]]\n"
     ]
    }
   ],
   "execution_count": 103
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T09:30:33.548668Z",
     "start_time": "2025-09-14T09:30:33.545493Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 * arr2",
   "id": "c9096bf6a5a65aa0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 1, 2],\n",
       "       [0, 2, 4]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 104
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "线性代数的乘法",
   "id": "f849efdef995102e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:00:26.373052Z",
     "start_time": "2025-09-14T10:00:26.366152Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 向量与向量\n",
    "arr1 = np.arange(5)\n",
    "arr2 = np.arange(5)\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "id": "cd2dfad91461dd51",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "[0 1 2 3 4]\n"
     ]
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:00:32.997918Z",
     "start_time": "2025-09-14T10:00:32.992341Z"
    }
   },
   "cell_type": "code",
   "source": "np.dot(arr1, arr2)",
   "id": "47744ede9794abae",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(30)"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 106
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:11:36.592199Z",
     "start_time": "2025-09-14T10:11:36.589031Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 向量与矩阵\n",
    "arr1 = np.arange(15).reshape((3, 5))\n",
    "arr2 = np.arange(5)\n",
    "print(arr1)\n",
    "print(arr2)"
   ],
   "id": "8a340f7784cae8b7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n",
      "[0 1 2 3 4]\n"
     ]
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:11:45.664406Z",
     "start_time": "2025-09-14T10:11:45.660643Z"
    }
   },
   "cell_type": "code",
   "source": "np.dot(arr1, arr2)  # 变成向量 原本应该是 3行1列的数据",
   "id": "b7e423406e023e9e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 30,  80, 130])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 109
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:14:23.223835Z",
     "start_time": "2025-09-14T10:14:23.215475Z"
    }
   },
   "cell_type": "code",
   "source": "np.dot(arr1, arr2).reshape((3, 1))",
   "id": "41c46735cce901a2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 30],\n",
       "       [ 80],\n",
       "       [130]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 110
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:32:39.484591Z",
     "start_time": "2025-09-14T10:32:39.474323Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = arr2.reshape((5, 1))\n",
    "print(arr2)"
   ],
   "id": "75fce973480847a5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]\n",
      " [4]]\n"
     ]
    }
   ],
   "execution_count": 116
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:32:45.948776Z",
     "start_time": "2025-09-14T10:32:45.945205Z"
    }
   },
   "cell_type": "code",
   "source": "print(arr1)",
   "id": "929b73a19f85fc8f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n"
     ]
    }
   ],
   "execution_count": 117
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:32:52.876419Z",
     "start_time": "2025-09-14T10:32:52.873569Z"
    }
   },
   "cell_type": "code",
   "source": "print(np.dot(arr1, arr2))",
   "id": "32d502d774e3e880",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 30]\n",
      " [ 80]\n",
      " [130]]\n"
     ]
    }
   ],
   "execution_count": 118
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:54:32.528495Z",
     "start_time": "2025-09-14T10:54:32.525210Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 找到数组的索引位置\n",
    "arr = np.random.normal(500, 70, 1000)\n",
    "print(np.where(arr > 650)[0])"
   ],
   "id": "4d7124e0c382880f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 93 124 274 277 341 385 435 449 514 666 672 701 716 799 827 926 958 961\n",
      " 990 992]\n"
     ]
    }
   ],
   "execution_count": 120
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-14T10:54:57.959347Z",
     "start_time": "2025-09-14T10:54:57.956659Z"
    }
   },
   "cell_type": "code",
   "source": "print(np.where(arr == np.max(arr))[0])",
   "id": "dbde39bc33a1325f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[449]\n"
     ]
    }
   ],
   "execution_count": 121
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "1fc85bba46543765"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
